/**
* This component is the glue of the program.
* Normal operation:
* <code>
* var afe = new AnnotationFileEditor(guiframe, toolapplet);
* afe.load('someschema');
* </code>
* 
*/

var AnnotationFileEditor = Class.create();
AnnotationFileEditor.prototype = {
/**
* @constructor
* Will initialize annotations and filemanager.
* @param {node} guiFrame The iframe that will contain the xforms-document. 
* @param {applet} toolApplet reference to the applet with the functions: 
*   public String transformEBSchema(String ebschema)
*   public String loadFile(String fileName)
*   public boolean saveFile(String fileName, String annotations)
*/
		initialize: function(guiFrame, toolApplet) {
		this.guiFrame	= guiFrame;
		this.guiDoc = null;
		this.toolApplet = toolApplet;
		this.annotations = new Annotations(this);
		this.fileManager = new FileManager(this);
		//guiFrame.document.write(toolApplet.transformEBSchema('schemas/PurchaseOrder.xsd'));
	},
/**
* Load a schema into the editor. 
* transforms the schema into an xfoms-document, loads the annotation file and
* calls load on the Annotations object.
*/
	load: function(schemaName) {
		var formdoc = this.toolApplet.transformEBSchema('schemas/'+schemaName+'.xsd');
		if (this.guiFrame.contentDocument) {
			this.guiFrame.contentDocument.open();
   		this.guiFrame.contentDocument.write(formdoc);
   		this.guiFrame.contentDocument.close();
   		this.guiDoc = this.guiFrame.contentDocument;
		} else {
			document.frames['guiframe'].document.open();
			document.frames['guiframe'].document.write(formdoc);
			document.frames['guiframe'].document.close();
			this.guiDoc = document.frames['guiframe'].document;
		}

		var ann = this.fileManager.getAnnotationFile(schemaName);
		this.annotations.load(ann, this.guiDoc);
	},
	/**
	 * Will print an error, but continue operation. 
	 * @param {String} errstr the error to output to the user.
	 */
	printError: function(errstr) {
		alert(errstr);
	},
	/**
	* Will print an error and trown an exception to stop the current thread.
	* @param {String} errstr The error to output to the user.
	* @param {Exception} The exception to throw.
	*/
	fatal: function(errstr, exception) {
		alert('fatal: '+errstr);
		throw exception;
	}
	
};

/**
* These are some error strings that will be used often. 
* Grouped here so they can be detailed and changed easily
*/
var EditorErrors = {
	browserSupport: 'Supported browsers: ff2 and ie7',
	securityError: 'Security error probably means that the application was not installed. Please run install.bat to grant the needed permissions to the editor',
	IOError: 'could not read or write to disk, data may be lost. please try again. '
}